package com.yx.pc.api.controller;


import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.Dto.TbRewardDto;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.entity.TbRewardRecord;
import com.yx.modules.api.entity.TbUser;
import com.yx.pc.api.service.PcTbRewardRecordService;
import com.yx.pc.api.service.PcTdBookService;
import com.yx.system.entity.TdBook;
import com.yx.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 打赏记录
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-12 14:34:41
 */
@RestController
@RequestMapping("/pc/api/rewardRecord")
public class PcTbRewardRecordController {

	@Autowired
	private PcTbRewardRecordService rewardRecordService;

	@Autowired
    private PcTdBookService tdBookService;
	
	/**
	 * 查看用户打赏记录列表
	 */
	@RequestMapping("/list")
	public R list(@LoginUser TbUser tbUser,@RequestParam Map<String, Object> params){
        Query query = new Query(params);
        query.put("userId",tbUser.getUserId());

		List<TbRewardRecord> rewardRecordList = rewardRecordService.queryList(query);
		int total = rewardRecordService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(rewardRecordList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("data", pageUtil);
	}

	/*
	* 查看打赏榜（周，月，年）
	*/
	@AuthIgnore
	@RequestMapping("/rewardList")
	public R rewardList(@RequestParam Map<String,Object> params){
		Date date = new Date();
		//判断自定义字段
		if (params.get("type")!=null){
			//type为1，获取总榜
			if (params.get("type").equals("1")){
//				params.put("rewardYear", DateUtil.getYear(date));
			}
			//type为2，获取月榜
			if (params.get("type").equals("2")){
				params.put("rewardYear", DateUtil.getYear(date));
				params.put("rewardMonth",DateUtil.getMonth(date));
			}
			//type为3，获取周榜
			if (params.get("type").equals("3")){
				params.put("rewardYear", DateUtil.getYear(date));
				params.put("rewardMonth",DateUtil.getMonth(date));
				params.put("rewardWeek",DateUtil.getWeek(date));
			}
		}
		Query query = new Query(params);
		List<TbRewardRecord> rewardRecordList = rewardRecordService.queryList1(query);
		int total = rewardRecordService.queryTotal1(query);
		//初始化数据传输集合
        List<TbRewardDto> tbRewardDtos = new ArrayList<>();
        for (TbRewardRecord tbRewardRecord:rewardRecordList){
            //初始化数据传输对象
            TbRewardDto tbRewardDto = new TbRewardDto();
            //查询图书对象
            TdBook tdBook = tdBookService.selectByPrimaryKey(tbRewardRecord.getBookId());
            //注入数据
            tbRewardDto.setTbRewardRecord(tbRewardRecord);
            tbRewardDto.setTdBook(tdBook);
            //加入集合
            tbRewardDtos.add(tbRewardDto);
        }
        PageUtils pageUtil = new PageUtils(tbRewardDtos, total, query.getLimit(), query.getPage());

		return R.ok().put("data",pageUtil);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		TbRewardRecord rewardRecord = rewardRecordService.queryObject(id);
		
		return R.ok().put("rewardRecord", rewardRecord);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TbRewardRecord rewardRecord){
		rewardRecordService.save(rewardRecord);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody TbRewardRecord rewardRecord){
		rewardRecordService.update(rewardRecord);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] ids){
		rewardRecordService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
